package org.yoki.edu.portal.web.utils;

import org.yoki.edu.common.protocol.portal.msg.PortalV1Msg;
import org.yoki.edu.common.protocol.portal.msg.PortalV2Msg;
import org.yoki.edu.common.protocol.portal.msg.attr.MsgAttr;
import org.yoki.edu.common.protocol.portal.msg.AbstractPortalMsg;

import java.util.ArrayList;
import java.util.List;

public class TlvTools {

    public static byte[] buildHwReqChallenge(int serialNo, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV2Msg b = new PortalV2Msg(2, 1, serialNo, 0, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }

    public static byte[] buildHwReqAuth(int serialNo, int reqId, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV2Msg b = new PortalV2Msg(2, 3, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }

    public static byte[] buildHwReqLogout(int serialNo, int reqId, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV2Msg b = new PortalV2Msg(2, 5, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }

    public static byte[] buildHwAffAckauth(int serialNo, int reqId, String userIp, List<MsgAttr> attr) {
        PortalV2Msg b = new PortalV2Msg(2, 7, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }

    public static byte[] buildHwReqInfo(int serialNo, int reqId, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV2Msg b = new PortalV2Msg(2, 9, serialNo, reqId, userIp, 0, attr.size(), attr, Constants.SECRET);
        return b.toByteArray();
    }

    /**
     * 通过字节码解析成HWTLVPortalMsg对象
     *
     * @param input
     * @return
     */
    public static PortalV2Msg parseHWTLVPortalMsg(byte[] input) {
        PortalV2Msg tlv = new PortalV2Msg();
        tlv.parse(input);
        return tlv;
    }

    public static byte[] buildReqChallenge(int serialNo, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV1Msg b = new PortalV1Msg(1, 1, serialNo, 0, userIp, 0, 0, attr);
        return b.toByteArray();
    }

    public static byte[] buildReqAuth(int serialNo, int reqId, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV1Msg b = new PortalV1Msg(1, 3, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }

    public static byte[] buildReqLogout(int serialNo, int reqId, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV1Msg b = new PortalV1Msg(1, 5, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }

    public static byte[] buildAffAckauth(int serialNo, int reqId, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV1Msg b = new PortalV1Msg(1, 7, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }

    public static byte[] buildReqInfo(int serialNo, int reqId, String userIp, List<MsgAttr> attr) {
        if (null == attr) {
            attr = new ArrayList<>();
        }
        PortalV1Msg b = new PortalV1Msg(1, 9, serialNo, reqId, userIp, 0, attr.size(), attr);
        return b.toByteArray();
    }

    /**
     * 通过字节码解析成DefaultTLVPortalMsg对象
     *
     * @param input
     * @return
     */
    public static AbstractPortalMsg parseDefaultTLVPortalMsg(byte[] input) {
        PortalV1Msg tlv = new PortalV1Msg();
        tlv.parse(input);
        return tlv;
    }
}
